package org.jeecg.modules.lab.items.relve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.lab.entrust.mapper.LabWentrustMapper;
import org.jeecg.modules.lab.items.entity.LabItemsBasisIndex;
import org.jeecg.modules.lab.items.entity.LabItemsRelevBasis;
import org.jeecg.modules.lab.items.item.mapper.LabItemsBasisIndexMapper;
import org.jeecg.modules.lab.items.relve.mapper.LabItemsRelevBasisMapper;
import org.jeecg.modules.lab.items.relve.service.ILabItemsRelevBasisService;
import org.jeecg.modules.lab.items.vo.LabItemsRelevBasisPage;
import org.jeecg.modules.lab.items.vo.LabItemsRelevPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 项目与标准关联表
 * @Author: jeecg-boot
 * @Date: 2024-01-03
 * @Version: V1.0
 */
@Service
public class LabItemsRelevBasisServiceImpl extends ServiceImpl<LabItemsRelevBasisMapper, LabItemsRelevBasis> implements ILabItemsRelevBasisService {

    @Autowired
    private LabItemsRelevBasisMapper labItemsRelevBasisMapper;

    @Autowired
    private LabItemsBasisIndexMapper labItemsBasisIndexMapper;

    @Override
    public void saveOrUpdate(LabItemsRelevPage labItemsRelevPage) {

        List<LabItemsRelevBasisPage> labItemsRelevBasisList = labItemsRelevPage.getLabItemsRelevBasisPageList();
        String itemId = labItemsRelevPage.getItemsId();
        for(LabItemsRelevBasisPage labItemsRelevBasisPage:labItemsRelevBasisList){
            String basisId = labItemsRelevBasisPage.getBasisId();
            //1、删除项目下标准
            QueryWrapper<LabItemsRelevBasis> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("items_id", itemId)
                    .eq("basis_id",basisId);
            labItemsRelevBasisMapper.delete(queryWrapper);

            //2、删除项目下指标
            QueryWrapper<LabItemsBasisIndex> indexWrapper = new QueryWrapper<>();
            indexWrapper.eq("items_id", itemId)
                    .eq("basis_id",basisId);
            labItemsBasisIndexMapper.delete(indexWrapper);
        }

        //3、重新插入标准和指标
        for (LabItemsRelevBasisPage labItemsRelevBasisPage : labItemsRelevBasisList) {
            LabItemsRelevBasis labItemsRelevBasis = new LabItemsRelevBasis();
            BeanUtils.copyProperties(labItemsRelevBasisPage, labItemsRelevBasis);
            labItemsRelevBasis.setSynFlag("0");
            labItemsRelevBasis.setId(null);
            labItemsRelevBasisMapper.insert(labItemsRelevBasis);
            List<LabItemsBasisIndex> labItemsBasisIndexList = labItemsRelevBasisPage.getLabItemsBasisIndexList();
            for (LabItemsBasisIndex index : labItemsBasisIndexList) {
                index.setSynFlag("0");
                index.setId(null);
                labItemsBasisIndexMapper.insert(index);
            }
        }
    }
}
